﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Kupa : Form
    {
        Controller cont;

        public Kupa()
        {
            cont = new Controller();
            InitializeComponent();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            grid_prod.Rows.Add(cont.FindPrice(Convert.ToInt32(txt_code.Text)) * Convert.ToInt32(txt_amount.Text), txt_amount.Text, cont.FindProdName(Convert.ToInt32(txt_code.Text)), txt_code.Text);
            txt_amount.Clear();
            txt_code.Clear();
        }

        private void btn_find_Click(object sender, EventArgs e)
        {
            txt_name.Text = cont.FindCustName(Convert.ToInt32(txt_ID.Text));
        }

        private void btn_buy_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter("bill.txt");
            
            sw.WriteLine(DateTime.Now);
            sw.WriteLine(txt_ID.Text + " " + txt_name.Text);
            double sum = 0;
            double cost = 0;
            int code = 0;
            int amount = 0;
            for (int i = 0; i < grid_prod.Rows.Count - 1; i++)
            {
                DataGridViewRow row = grid_prod.Rows.SharedRow(i);
                code = Convert.ToInt32(row.Cells["code"].Value);
                amount = Convert.ToInt32(row.Cells["amount"].Value);
                cost = Convert.ToDouble(row.Cells["price"].Value);
                sum += cost;
                cont.AddShopping(Convert.ToInt32(txt_ID.Text), code, amount, cost);
                   // MessageBox.Show("הודעה למנהל: מוצר חסר במלאי");

                sw.WriteLine("" + code + " " + row.Cells["name"].Value + " " + amount + " " + cost);

                
            }
            sw.WriteLine("total- " + sum);
            sw.Close();
            txt_ID.Clear();
            txt_name.Clear();
            txt_code.Clear();
            txt_amount.Clear();
            int n = grid_prod.Rows.Count - 1;
            for (int i = 0; i < n; i++)
            {
                grid_prod.Rows.Remove(grid_prod.Rows.SharedRow(0));
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            int n = grid_prod.Rows.Count - 1;
            for (int i = 0; i < n; i++)
            {
                grid_prod.Rows.Remove(grid_prod.Rows.SharedRow(i));
            }
            txt_ID.Clear();
            txt_name.Clear();
            txt_code.Clear();
            txt_amount.Clear();
        }
    }
}
